<?php
if (!defined('IN_IA')) {
	die('Access Denied');
}
global $_W, $_GPC;
$openid = m('user')->getOpenid();
$member = m('member')->getInfo($openid);
$op = empty($_GPC['op']) ? 'display' : $_GPC['op'];
if($op == 'display'){
    $user=pdo_fetch("select * from".tablename("ewei_shop_member")." where uniacid=:uniacid and openid=:openid ",array(":uniacid"=>$_W['uniacid'],":openid"=>$openid));
    $order=pdo_fetch("select * from".tablename("franchise_apply_order")."where uniacid=:uniacid and openid=:openid and status=2 ",array(":uniacid"=>$_W['uniacid'],":openid"=>$openid));
    if(!empty($order['sendtime'])){
     $shenhe=date("Y-m-d H:i",$order['sendtime']); 
    }else{
     $shenhe="";   
    }
    include $this->template('franchise/center');
}
if($op == 'program'){
   include $this->template('franchise/program');
}
if($op == 'promote'){
   include $this->template('franchise/promote');
}
if($op == 'class_join'){
   if(empty($_GPC['id'])){
       exit;
   }
   $lesson=pdo_fetch("select * from".tablename("franchise_lesson")." where uniacid=:uniacid and id=:id ",array(':uniacid'=>$_W['uniacid'],":id"=>$_GPC['id']));
   $lesson['starttime']=date("Y-m-d",$lesson['starttime']);
   include $this->template('franchise/class_join');  
}
if($op == 'activity'){
   $activity=pdo_fetchall("select * from".tablename("franchise_activity")." where uniacid=:uniacid and status=1 ",array(':uniacid'=>$_W['uniacid']));
   include $this->template('franchise/activity'); 
}
if($op =='decorate'){
   $case=pdo_fetchall("select * from ".tablename("franchise_decorate_case")." where uniacid=:uniacid and status=1 order by id asc",array(":uniacid"=>$_W['uniacid']));
   include $this->template('franchise/decorate'); 
}
if($op =='decorate_detail'){
    $id=intval($_GPC['id']);
   if(empty($id)){
   exit;
   }
   $case=pdo_fetch('select * from '.tablename('franchise_decorate_case')." where uniacid=:uniacid and id=:id",array(':uniacid'=>$_W['uniacid'],':id'=>$id));
   include $this->template('franchise/decorate_detail'); 
}
if($op =='decorate_need'){
    if (empty($_GPC['id'])) {
        exit;
    }
    if (!empty($_GPC['data'])) {
        $data = $_GPC['data'];
        $data['uniacid'] = $_W['uniacid'];
        $data['openid'] = $openid;
        $data['case_id']=$_GPC['id'];
        $data['createtime'] = time();
        $data['starttime']=strtotime($data['starttime']);
        pdo_insert('franchise_decorate_need', $data);
        $recordid = pdo_insertid();
        if(!empty($recordid)){
        show_json(1); 
        }else{
        show_json(0);    
        }
    }
   include $this->template('franchise/decorate_need');
}
if($op == "class_up"){
   $lesson=pdo_fetchall("select * from".tablename('franchise_lesson')."where uniacid=:uniacid and status=1 and type=1 order by id asc",array(':uniacid'=>$_W['uniacid']));
   foreach($lesson as $key=>$value){
   $lesson[$key]['nav']=$value['id']%4;
   }
   include $this->template('franchise/class_up');
}
if($op == "class_down"){
   $lesson=pdo_fetchall("select * from".tablename('franchise_lesson')."where uniacid=:uniacid and status=1 and type=2 order by id asc",array(':uniacid'=>$_W['uniacid']));
   foreach($lesson as $key=>$value){
   $lesson[$key]['nav']=$value['id']%4;
   }
   include $this->template('franchise/class_down');
}
if($op == "class_detail"){
   if(empty($_GPC['id'])){
       exit;
   }
   $lesson=pdo_fetch("select * from".tablename("franchise_lesson")." where uniacid=:uniacid and id=:id ",array(':uniacid'=>$_W['uniacid'],":id"=>$_GPC['id']));
   $lesson['total']=$lesson['join_num']+$lesson['em_num'];
   include $this->template('franchise/class_detail');
}
if($op == "class_submit"){
    if(empty($_GPC['id'])){
    show_json(0);
    exit;
    }
   $lesson=pdo_fetch("select * from".tablename("franchise_lesson")." where uniacid=:uniacid and id=:id ",array(':uniacid'=>$_W['uniacid'],":id"=>$_GPC['id']));
   $less=pdo_fetch("select * from ".tablename("franchise_lesson_join_info")." where uniacid=:uniacid and meet_id=:meet_id and openid=:openid ",array(":uniacid"=>$_W['uniacid'],":openid"=>$openid,":meet_id"=>$_GPC['id']));
   if(!empty($less)){
    show_json(-1);
    exit;
   }
   $data=$_GPC['data'];
   $data['uniacid']=$_W["uniacid"];
   $data["openid"]=$openid;
   $data["meet_id"]=$_GPC["id"];
   $data["createtime"]=time();
   pdo_insert("franchise_lesson_join_info",$data);
   $lessonid = pdo_insertid();
   if(!empty($lessonid)){
     //报名人数加一
     $join_num=$lesson['join_num']+1;
     pdo_update("franchise_lesson",array("join_num"=>$join_num),array('uniacid' => $_W['uniacid'], 'id' =>$lesson['id']));
     //发生报名成功的推送消息！
     $msg = array(
     'first' => array('value' => "您已成功报名{$lesson['title']}课程！", "color" => "#4a5077"),
     'time' => array('title' => '课程时间', 'value' => date("Y-m-d H:i",$lesson['starttime']), "color" => "#4a5077"),
     'address' => array('title' => '课程地址', 'value' =>$lesson['address'], "color" => "#4a5077"),
     'remark' => array('value' => "请记得按时参加哦~更多资讯尽在小妖家!", "color" => "#4a5077")
     );
     m('message')->sendCustomNotice($openid, $msg);
    show_json(1); 
    }else{
    show_json(0);    
    }
 }
 if($op == "activity_submit"){
    if(empty($_GPC['activity_id'])){
    show_json(0);
    exit;
    }
   $activity=pdo_fetch("select * from ".tablename("franchise_activity_join")." where uniacid=:uniacid and activity_id=:activity_id and openid=:openid ",array(":uniacid"=>$_W['uniacid'],":openid"=>$openid,":activity_id"=>$_GPC['activity_id']));
   if(!empty($activity)){
    show_json(-1);
    exit;
   }
   $data['uniacid']=$_W["uniacid"];
   $data["openid"]=$openid;
   $data["activity_id"]=$_GPC["activity_id"];
   $data["createtime"]=time();
   pdo_insert("franchise_activity_join",$data);
   $join_id = pdo_insertid();
    if(!empty($join_id)){
    show_json(1); 
    }else{
    show_json(0);    
    }
 }
 if($op == "program_submit"){
    if(empty($_GPC['program_id'])){
    show_json(0);
    exit;
    }
   $program=pdo_fetch("select * from ".tablename("franchise_program_join")." where uniacid=:uniacid and program_id=:program_id and openid=:openid ",array(":uniacid"=>$_W['uniacid'],":openid"=>$openid,":program_id"=>$_GPC['program_id']));
   if(!empty($program)){
    show_json(-1);
    exit;
   }
   $data['uniacid']=$_W["uniacid"];
   $data["openid"]=$openid;
   $data["program_id"]=$_GPC["program_id"];
   $data["createtime"]=time();
   pdo_insert("franchise_program_join",$data);
   $join_id = pdo_insertid();
    if(!empty($join_id)){
    show_json(1); 
    }else{
    show_json(0);    
    }
 }
  if($op == "promote_submit"){
    if(empty($_GPC['promote_id'])){
    show_json(0);
    exit;
    }
   $promote=pdo_fetch("select * from ".tablename("franchise_promote_join")." where uniacid=:uniacid and promote_id=:promote_id and openid=:openid ",array(":uniacid"=>$_W['uniacid'],":openid"=>$openid,":promote_id"=>$_GPC['promote_id']));
   if(!empty($promote)){
    show_json(-1);
    exit;
   }
   $data['uniacid']=$_W["uniacid"];
   $data["openid"]=$openid;
   $data["promote_id"]=$_GPC["promote_id"];
   $data["createtime"]=time();
   pdo_insert("franchise_promote_join",$data);
   $join_id = pdo_insertid();
    if(!empty($join_id)){
    show_json(1); 
    }else{
    show_json(0);    
    }
 }